/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.util;

import net.sourceforge.stripes.validation.DateTypeConverter;
import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class BetterDateTypeConverter extends DateTypeConverter {
  private static final Pattern TIME_ONLY = Pattern.compile("\\d{1,2}:\\d{1,2}(:\\d{1,2})?( (AM|PM))?", Pattern.CASE_INSENSITIVE);
  private DateFormat[] allFormats;

  public BetterDateTypeConverter() {
    super();
  }

  protected DateFormat[] getDateFormats() {
    if (allFormats == null) {
      List<DateFormat> allMyFormats = new ArrayList<DateFormat>();

      allMyFormats.add(new SimpleDateFormat("yyyy MM dd h:m:s a"));
      allMyFormats.add(new SimpleDateFormat("yyyy MM dd h:m a"));
      allMyFormats.add(new SimpleDateFormat("yyyy MM dd H:m:s"));
      allMyFormats.add(new SimpleDateFormat("yyyy MM dd H:m"));

      allMyFormats.add(new SimpleDateFormat("MMM d yyyy h:m:s a"));
      allMyFormats.add(new SimpleDateFormat("MMM d yyyy h:m a"));
      allMyFormats.add(new SimpleDateFormat("MMM d yyyy H:m:s"));
      allMyFormats.add(new SimpleDateFormat("MMM d yyyy H:m"));

      allMyFormats.add(new SimpleDateFormat("MMMM d yyyy h:m:s a"));
      allMyFormats.add(new SimpleDateFormat("MMMM d yyyy h:m a"));
      allMyFormats.add(new SimpleDateFormat("MMMM d yyyy H:m:s"));
      allMyFormats.add(new SimpleDateFormat("MMMM d yyyy H:m"));

      allMyFormats.addAll(Arrays.asList(super.getDateFormats()));

      allMyFormats.add(new ChronicDateFormat("yyyy-MM-dd HH:mm:ss"));
      allFormats = allMyFormats.toArray(new DateFormat[0]);
    }
    return allFormats;
  }

  /**
   * Overridden to check for strings that represent only time.  If this is the case, they are prepended with the
   * current date and passed along.
   * @param string string to check
   * @return a fully processed string guaranteed to be in approximately the right format.  :-)
   */
  @Override
  protected String preProcessInput(String string) {
    if (StringUtils.isNotBlank(string) && TIME_ONLY.matcher(string.trim()).matches()) {
      string = new StringBuilder(new SimpleDateFormat("yyyy MM dd").format(new Date())).append(' ').append(string).toString();
    }
    return super.preProcessInput(string);
  }
}
